package com.library.admin.controller;

import com.library.admin.modules.book.entity.Book;
import com.library.admin.modules.book.vo.BookVO;
import com.library.admin.modules.book.bo.BookPage;
import com.library.admin.modules.book.bo.BookInsert;
import com.library.admin.modules.book.bo.BookUpdate;
import com.library.admin.modules.book.service.BookService;
import com.library.common.annotation.CurrentUser;
import com.library.common.service.bo.CurrentLoginUser;
import com.library.logging.annotation.LogSys;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.common.response.Result;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.util.List;
import java.util.Objects;


/**
 * @Author xyh
 * @Desc 图书表(Book)表控制层
 * @Date 2023-10-05 17:27:46
 */

@RestController
@RequestMapping("book")
public class BookController {

    @Resource
    private BookService bookService;


    /**
     * 分页查询列表
     *
     * @return 数据
     */
    @LogSys(value = "图书分页查询列表")
    @GetMapping("/list")
    public Result<IPage<BookVO>> queryByPage(@Valid BookPage page) {
        return Result.success(bookService.queryByPage(page));
    }

    /**
     * 根据ID获取数据
     *
     * @return 单个图书表数据
     */
    @LogSys(value = "根据图书ID获取数据")
    @PostMapping("/queryById/{id}")
    public Result<BookVO> queryById(@PathVariable("id") Integer id) {
        return Result.success(bookService.queryById(id));
    }


    /**
     * 添加图书表
     *
     * @return 新增图书表数据
     */
    @LogSys(value = "新增图书表数据")
    @PostMapping("/insert")
    public Result insert(@Valid @RequestBody BookInsert param, @CurrentUser CurrentLoginUser currentLoginUser) {
        param.setUsername(currentLoginUser.getUsername());
        bookService.insert(param);
        return Result.success();
    }


    /**
     * 编辑图书表
     *
     * @return 编辑图书表数据
     */
    @LogSys(value = "编辑图书表数据")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody BookUpdate param) {
        if (Objects.isNull(param.getId())) {
            return Result.error("ID不能为空");
        }
        bookService.update(param);
        return Result.success();
    }

    /**
     * 删除图书表
     *
     * @return 删除图书表数据
     */
    @LogSys(value = "删除图书表数据")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        bookService.deleteById(id);
        return Result.success();
    }

    /**
     * 图书筛选下拉数据展示
     * @return
     */
    @LogSys(value = "图书筛选下拉数据展示")
    @GetMapping("/select")
    public Result<List<Book>> getBookList() {
        List<Book> bookList = bookService.getBookList();
        return Result.success(bookList);
    }

}
